package com.zheng.study.web.controller.main.company;

import com.zheng.study.base.bean.BaseEnumManager;
import com.zheng.study.base.bean.search.SearchEnum;
import com.zheng.study.base.bean.search.order.Order;
import com.zheng.study.base.bean.search.result.SearchResultPaging;
import com.zheng.study.base.bean.search.rule.Rule;
import com.zheng.study.base.bean.search.rule.RuleHelper;
import com.zheng.study.company.entity.Post;
import com.zheng.study.company.entity.User;
import com.zheng.study.company.service.PostService;
import com.zheng.study.web.controller.BaseController;
import com.zheng.study.web.shiro.bind.annotation.CurrentUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * newFile
 * ============================================================================
 * author : dzw
 * createDate:  2018/11/5 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/main/post")
public class PostController extends BaseController {
	private static final long serialVersionUID = 6076131830669210817L;

	@Autowired
	private PostService postService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> list(@CurrentUser User user, Integer draw){
		Order order = Order.order("pinYinHead", SearchEnum.OrderType.asc);
		List<Rule> rules = new ArrayList<Rule>();
		rules.add(RuleHelper.eq("company", user.getCompany()));
		rules.add(RuleHelper.eq("state", BaseEnumManager.StateEnum.Enable));
		SearchResultPaging searchResult = postService.findPagingBySearch(null, rules, order);
		List<Post> postList = searchResult.getList();

		Map<String, Object> data = new HashMap<String, Object>();
		List<Map<String, Object>> dataRows = new ArrayList<Map<String, Object>>();
		Map<String, Object> rMap;
		for(Post post:postList){
			rMap = new HashMap<String, Object>();
			rMap.put("id", StringUtils.isEmpty(post.getId())?"":post.getId());
			rMap.put("name", StringUtils.isEmpty(post.getName())?"":post.getName());
			rMap.put("description", StringUtils.isEmpty(post.getDescription())?"":post.getDescription());
			dataRows.add(rMap);
		}

		data.put("data", dataRows);
		data.put("draw", draw);
		data.put("recordsTotal", searchResult.getTotalCount());
		data.put("recordsFiltered", searchResult.getTotalCount());
		return data;
	}

	/**
	 * input dialog
	 */
	@RequestMapping(value = "input", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
	public String input(Model model, String keyId){
		if(StringUtils.isNotEmpty(keyId)){
			model.addAttribute("post", postService.get(keyId));
		}
		return "/main/company/post_input";
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(@CurrentUser User user, String keyId, String name, String description){
		if(StringUtils.isBlank(name)){
			return resultService.fail("请填写岗位名称");
		}

		Post post;
		if(StringUtils.isNotEmpty(keyId)){
			post = postService.get(keyId);
		}else{
			post = new Post();
			post.setCreater(user);
			post.setCompany(user.getCompany());
		}
		post.setName(name);
		post.setDescription(description);

		if(StringUtils.isNotEmpty(keyId)){
			postService.update(post);
		}else{
			keyId = postService.save(post);
		}

		return resultService.succesWithId(keyId);
	}

}
